
#include "StdAfx.h"
#include "RenderManager.h"

#include "IrrRender.h"


#include "../Kernel.h"

extern Kernel* g_kernel;


/** Create a Renderer */
IRender* CreateRenderer( Renderer_t type )
{
    IRender* r = NULL;
    switch( type )
    {
    case VF_IRRLICHT_DX8:
    case VF_IRRLICHT_DX9:
    case VF_IRRLICHT_OPENGL:
    case VF_IRRLICHT_SW:
    case VF_IRRLICHT_BV:
        r = new IrrRender;
        break;
    default:
        ;   // TODO
    };
    return r;
}

/** Initialize the render manager */
int RenderManager::Init( const ViewSettings &settings )
{
    m_renderer = CreateRenderer( settings.GetRenderer() );
    if ( ! m_renderer )
    {
        g_kernel->LPrintf( "*** FATAL ERROR - Unable to create rendering engine! ***" );
        return 1;
    }
    if ( m_renderer->Init( settings ) )
    {
        g_kernel->LPrintf( "*** FATAL ERROR - Unable to cr eate rendering engine! ***" );
        return 1;
    }

    return 0;
}

/** Update the gamestate views */
void RenderManager::Update(float  dt)
{
    Render( dt );
}

/** Close off */
void RenderManager::Shutdown()
{
    if ( m_renderer )
    {
        m_renderer->Shutdown();
        delete m_renderer;
        m_renderer = 0;
    }
}

/** Render the scene */
void RenderManager::Render(float dt)
{
    m_renderer->PreRender();
    type_StateView::iterator it = m_stateList.begin();
    for (; it != m_stateList.end(); ++it )
    {
        if ( (*it)->IsActive() )
            (*it)->Render( m_renderer, NULL );
    }

    m_renderer->PostRender();
}


